package com.songhq.netty.two;




import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;
import io.netty.util.CharsetUtil;
import io.netty.channel.ChannelHandler.Sharable;//导入ChannelHandler内部注解

@Sharable
public class EchoServerHandler  extends ChannelHandlerAdapter{
	//对每个消息的传入都调用
	@Override
	public void channelRead(ChannelHandlerContext ctx, Object msg){
		//SimpleLeakAwareByteBuf
		ByteBuf buff=(ByteBuf)msg;
		System.out.println("接收到了"+buff.toString(CharsetUtil.UTF_8));
		ctx.writeAndFlush(Unpooled.copiedBuffer("你好，我是服务端",CharsetUtil.UTF_8));
	}
	//最后一次调用
	@Override
	public void channelReadComplete(ChannelHandlerContext ctx){
		//关闭连接
		ctx.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
		
	}
	@Override
	public void exceptionCaught(ChannelHandlerContext ctx,Throwable cause){
		cause.printStackTrace();
		ctx.close();
		
	}
	
	
	
	
}
